/********************************************************/
/*	Author:		gong_libin			*/
/*	Date:		2017_11_01			*/
/*	File:		CmAdmin.cpp			*/
/********************************************************/

#include "CmAdmin.h"

void CCmAdmin::CmThreadCore()
{
	CmAdminLoad();

	while (CM_SUCCESS == sleep(CM_ADM_SLEEP)) {
		for (ULONG ulTrans = 0; ulTrans < m_rCMapper.CmMapSize(); ++ ulTrans) {
			CCmTrans* pCTrans = *(m_ppCTrans + ulTrans);
			CMSTR_C CVal = "", CKey(pCTrans->CmTransName());
			m_rCMapper.CmMapSelect(CKey, CVal, sizeof(CMSTR_C));
			for (UINT uiLink = 0; uiLink < CM_LNK_NUM; ++ uiLink) {
				if (CM_LNK_ZERO == (pCTrans->CmTransLink() + uiLink)->m_cTag) {
					pCTrans->CmTransConnectOne(pCTrans->CmTransLink() + uiLink, (char*)CVal.c_str());
				}
			}
		}
		CmAdminPrint();
	}

	return;
}

void CCmAdmin::CmAdminLoad()
{
	ULONG ulIndex = 0;

	for (CMMAP_C::CM_ITE CIte = m_rCMapper.CmMapBegin(); CIte != m_rCMapper.CmMapEnd(); ++ CIte) {
		CCmTrans* pCTrans = new CCmTrans((char*)CIte->first.c_str());
		memcpy(m_ppCTrans + ulIndex, &pCTrans, sizeof(CCmTrans*));
		pCTrans->CmTransConnectAll((char*)CIte->second.c_str());
		ulIndex += 1;
	}

	CmAdminPrint();

	return;
}

void CCmAdmin::CmAdminPrint()
{
	for (ULONG ulTrans = 0; ulTrans < m_rCMapper.CmMapSize(); ++ ulTrans) {
		CCmTrans* pCTrans = *(m_ppCTrans + ulTrans);
		CM_PRINT("\"%s\" -> {\n", pCTrans->CmTransName());
		for (UINT uiLink = 0; uiLink < CM_LNK_NUM; ++ uiLink) {
			CM_PRINT("\t{\"Idx\": %03u, ", uiLink);
			CMLINK_S* pstLink = pCTrans->CmTransLink() + uiLink;
			CM_PRINT("\"Skt\": %d, \"Tag\": %d},\n", pstLink->m_CTcp.CmNetGetSocket(), pstLink->m_cTag);
		}
		CM_PRINT("};\n");
	}

	return;
}
